/*
 * @Author: wangyifei 844650674@qq.com
 * @Date: 2022-11-27 10:22:27
 * @LastEditors: wangyifei 844650674@qq.com
 * @LastEditTime: 2022-11-27 13:45:55
 * @FilePath: \management_system_template\src\service.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
export function ajaxRequest(url, method, params) {
  return new Promise((resolve, reject) => {
    let data;
    if (method === "POST" || method === "PUT") {
      data = JSON.stringify(params);
    } else {
      data = null;
    }
    $.ajax({
      type: method,
      url,
      data,
      contentType: "application/json;charset=utf-8",
      beforeSend: function(request) {
        if (localStorage.getItem("userInfo")) {
          request.setRequestHeader(
            "Authorization",
            JSON.parse(localStorage.getItem("userInfo")).token
          );
        }
      },

      success: (res) => {
        resolve(res);
      },
      error: (jqXHR, textStatus, errorThrown) => {
        reject(textStatus);
      },
    });
  });
}

// 登录
export const login = (params) => {
  return ajaxRequest("/users/login", "POST", params);
};

// 注册
export const register = (params) => {
  return ajaxRequest("/users", "POST", params);
};

// 编辑用户
export const updateUser = (id, params) => {
  return ajaxRequest(`/user/${id}`, "POST", params);
};

// 新建用户
export const addUser = (params) => {
  return ajaxRequest("/users", "POST", params);
};

// 获取用户列表
export const getUser = (params) => {
  return ajaxRequest("/users", "GET", params);
};

// getUserInfo
export const getUserInfo = (id) => {
  return ajaxRequest(`/user/${id}`, "GET");
};

export const hasAdmin = (params) => {
  return ajaxRequest(`/users/hasAdmin`, "POST", params);
}

// 删除用户
export const deleteUser = (id) => {
  return ajaxRequest(`/user/${id}`, "DELETE");
}

// 获取模板信息
export const getModel = () => {
  return ajaxRequest(`/model`, "GET");
}

// 新建模板
export const addModel = (params) => {
  return ajaxRequest(`/model`, "POST", params);
};


export const deleteModel = (id) => {
  return ajaxRequest(`/model/${id}`, "DELETE");
}

// 编辑用户
export const updateModel = (id, params) => {
  return ajaxRequest(`/model/${id}`, "POST", params);
};


// 玩乐点相关接口
export const addFun = (params) => {
  return ajaxRequest(`/fun`, "POST", params);
};
export const getFun = (params) => {
  return ajaxRequest(`/fun`, "GET", params);
};

export const deleteFun = (id) => {
  return ajaxRequest(`/fun/${id}`, "DELETE");
};

export const updateFun = (id, params) => {
  return ajaxRequest(`/fun/${id}`, "POST", params);
};



// 角色相关接口
export const addRole = (params) => {
  return ajaxRequest(`/role`, "POST", params);
};
export const getRole = (params) => {
  return ajaxRequest(`/role`, "GET", params);
};

export const deleteRole = (id) => {
  return ajaxRequest(`/role/${id}`, "DELETE");
};

export const updateRole = (id, params) => {
  return ajaxRequest(`/role/${id}`, "POST", params);
};
